package reggie.common;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 */
@ControllerAdvice(annotations ={RestController.class, Controller.class})
@ResponseBody
public class GlobalExceptionHandler {
    /**
     * 套餐删除异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(customException.class)
    public R<String> exceptionHandler(customException e){
        return R.error(e.getMessage());
    }

    /**
     * 用户异常处理
     * @param sql
     * @return
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> SqlexceptionHandler(SQLIntegrityConstraintViolationException sql){
        if (sql.getMessage().contains("Duplicate entry")){
            String[] s = sql.getMessage().split(" ");
            String msg = s[2] +"已存在";
            return R.error(msg);
        }
        return R.error("未知错误");
    }
}
